package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.POSLogic;
import com.hotel.BL.UserLevelLogic;

import data.POS;
import data.UserLevel;
import data.passingData.ResultData;


@SuppressWarnings("serial")
public class UserLevelService extends HttpServlet {
	//http://localhost:8080/userlevelService?func=GetStaff&department=HouseKeeping
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addUserLevel")){
				AddUserLevel(req,resp);
			}else if(doClass.equals("editUserLevel")){
				EditUserLevel(req,resp);
			}else if(doClass.equals("delUserLevel")){
				DeleteUserLevel(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("getAllUserLevel4Admin")){
				GetAllUserLevel4Admin(req,resp);
			}else if(doClass.equals("getUserLevelById")){
				GetUserLevelById(req,resp);
			}else if(doClass.equals("getPOS")){
				GetPOS(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddUserLevel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String workarea = req.getParameter("workarea");
		String defaultpoint = req.getParameter("defaultpoint");
		boolean canShort = Boolean.parseBoolean(req.getParameter("canShort"));
		boolean adminAllowed = Boolean.parseBoolean(req.getParameter("adminAllowed"));
		boolean housekeepingAllowed = Boolean.parseBoolean(req.getParameter("housekeepingAllowed"));
		new UserLevelLogic(req,resp).AddUserLevel(hotelId,title,defaultpoint,workarea,canShort,adminAllowed,housekeepingAllowed);
		
		GetAllUserLevel4Admin(req, resp);
	}

	private void EditUserLevel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long id = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		String workarea = req.getParameter("workarea");
		String defaultpoint = req.getParameter("defaultpoint");
		boolean canShort = Boolean.parseBoolean(req.getParameter("canShort"));
		new UserLevelLogic(req,resp).EditUserLevel(id,title,defaultpoint,workarea,canShort);

		GetAllUserLevel4Admin(req, resp);

	}

	private void DeleteUserLevel(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String id = req.getParameter("id");
		UserLevelLogic logic = new UserLevelLogic(req, resp);
		for(int i = 0; i < id.split(",").length ; i++){
			logic.DeleteUserLevel(Long.parseLong(id.split(",")[i]));
		}

		GetAllUserLevel4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long salutationId = Long.parseLong(req.getParameter("id"));

		Object obj = new UserLevelLogic(req,resp).ChangeStatus(salutationId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllUserLevel4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		UserLevelLogic userLevelLogic = new UserLevelLogic(req,resp);
		
		List<UserLevel> obj = userLevelLogic.GetAllUserLevel4AdminLimit(hotelId,offset,line);
		double dataSize = userLevelLogic.GetAllUserLevel4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", userLevelLogic.SetToData(obj));
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetUserLevelById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long id = Long.parseLong(req.getParameter("id"));
		List<POS> pos = new POSLogic(req,resp).GetAllPOS(hotelId);
		UserLevel note = new UserLevelLogic(req,resp).GetUserLevel(id);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("POS", new POSLogic(req,resp).OnlyIdAndTitle(pos));
		map.put("UserLevel", note);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(map));
	}

	private void GetPOS(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<POS> pos = new POSLogic(req,resp).GetAllPOS(hotelId);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(new POSLogic(req,resp).OnlyIdAndTitle(pos)));
	}
	
}
